home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / pascal / pcxkt3.zip / HOEDOWN.C < prev    next >
C/C++ Source or Header  |  1991-12-28  |  4KB  |  121 lines

  1. /*
  2.    HOEDOWN.C by Peter Donnelly
  3.  
  4.    Demonstration of animation against a backdrop. See CLIP.DOC.
  5.  
  6.    Before compiling and running the program, set the PATHTODRIVER constant 
  7.    to the directory where your .BGI files reside.
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <graphics.h>
  12. #include <conio.h>
  13.  
  14. #define PATHTODRIVER "C:\\TC\\BGI"
  15. #define DATAFILE "HOEDOWN.DTA"
  16. #define STEP 10                     /* Governs lateral motion of figure */
  17. #define YPOS 100                    /* Top line of images */
  18.  
  19. typedef struct {
  20.       int            width, height, size, xpos;
  21.       unsigned char  *image, *backdrop;
  22.       } IMAGETYPE;
  23.  
  24.  
  25. FILE                *icon_file;
  26. int                 grdriver = EGA, grmode = EGAHI, grerror;
  27. struct palettetype  pal;
  28. int                 x, page;
  29. IMAGETYPE           Clem[2], bales;
  30. char                junk;
  31.  
  32. /* ---------------- Function to read an image from file ------------- */
  33.  
  34. void get_icon(IMAGETYPE *pic)
  35.   {
  36.   int size;
  37.  
  38.   fread(&(pic->width), 2, 1, icon_file);          /* Get dimensions first */
  39.   fread(&(pic->height), 2, 1, icon_file);
  40.   size = imagesize(0, 0, pic->width, pic->height);
  41.   pic->image = (unsigned char*) malloc(size);     /* Allocate dynamic memory */
  42.   pic->backdrop = (unsigned char*) malloc(size);
  43.   fseek(icon_file, -4, SEEK_CUR);                 /* Back up in the file */
  44.   fread(pic->image, size, 1, icon_file);          /* Store the image */
  45.   }
  46.  
  47. /* ----------------------------------------------------------------------- */
  48.  
  49. void main()  {
  50.  
  51. /* Open data file and quit if not found. The file consists of the palette
  52.    record followed by three images. */
  53.  
  54. if ((icon_file = fopen(DATAFILE, "rb")) == NULL) {
  55.   printf("File %s not found.\n",DATAFILE);
  56.   exit(1);
  57.   }
  58.  
  59. /* Initialize graphics */
  60.  
  61. initgraph(&grdriver, &grmode, PATHTODRIVER);
  62. grerror = graphresult();
  63. if (grerror) {
  64.   printf("Graphics error: %s\n",grapherrormsg(grerror));
  65.   exit(1);
  66.   }
  67.  
  68. /* Read the palette from file and set the colors */
  69.  
  70. fread(&pal, sizeof(pal), 1, icon_file);
  71. setallpalette(&pal);
  72.  
  73. /* Initialize the position of the images */
  74.  
  75. Clem[1].xpos = 590;
  76. Clem[0].xpos = Clem[1].xpos - STEP / 2;
  77.  
  78. /* Get the images into memory */
  79.  
  80. for (x = 0; x < 2; x++) get_icon(&Clem[x]);
  81. get_icon(&bales);
  82. fclose(icon_file);
  83.  
  84. /* Draw the scenic backdrop on both video pages, and store the part
  85.    that will be written to the screen when the loop is entered */
  86.  
  87. for (page = 0; page < 2; page++) {
  88.   bales.xpos = 20;
  89.   setactivepage(page);
  90.   setvisualpage(page ^ 1);
  91.   do {
  92.     putimage(bales.xpos, YPOS, bales.image, COPY_PUT);
  93.     bales.xpos += bales.width;
  94.     } while (bales.xpos < getmaxx());
  95.   getimage(Clem[page].xpos, YPOS, Clem[page].xpos+Clem[page].width,
  96.        YPOS+Clem[page].height, Clem[page].backdrop);
  97.   }
  98.  
  99. /* Now flip between the two video pages. While one page is being viewed,
  100.    update the other. First copy the stored backdrop over the dancing
  101.    figure, then increment his position, then store the backdrop at
  102.    his new position, and finally redraw him. */
  103.  
  104. page = 1;
  105. while (!kbhit()) {
  106.   setactivepage(page);
  107.   setvisualpage(page ^ 1);
  108.   putimage(Clem[page].xpos, YPOS, Clem[page].backdrop, COPY_PUT);
  109.   Clem[page].xpos -= STEP;
  110.   if (Clem[page].xpos < 0) Clem[page].xpos = 590;
  111.   getimage(Clem[page].xpos, YPOS,
  112.        Clem[page].xpos + Clem[page].width, YPOS + Clem[page].height,
  113.        Clem[page].backdrop);
  114.   putimage(Clem[page].xpos, YPOS, Clem[page].image, OR_PUT);
  115.   delay(200);
  116.   page^= 1;
  117.   }
  118. junk = getch();                        /* Discard keypress */
  119. closegraph();
  120. }
  121.